import React from 'react';
import { Table, Form } from 'antd';
import LimitInput from '@/components/LimitInput';
import styles from './index.less';

const initialList = [
    {
        value: 5,
        label: ''
    },
    {
        value: 4,
        label: ''
    },
    {
        value: 3,
        label: ''
    },
    {
        value: 2,
        label: ''
    },
    {
        value: 1,
        label: ''
    }
];

const EvaluationItemTable = ({ form, dataSource = [], onChange, ...restProps }) => {
    const { getFieldDecorator } = form;

    const editColumns = [
        {
            title: '评分说明',
            dataIndex: 'label',
            render: (text, record) => (
                <Form.Item style={{ margin: 0, width: '100%' }}>
                    {getFieldDecorator(`label${record?.value}`, {
                        initialValue: record?.label || ''
                    })(
                        <LimitInput
                            area
                            maxLength={1000}
                            placeholder="请输入评分标准"
                            autoSize={{ minRows: 1, maxRows: 5 }}
                            style={{ marginBottom: 4 }}
                        />
                    )}
                </Form.Item>
            )
        },
        {
            title: '分数',
            dataIndex: 'value',
            width: 87
        }
    ];

    const getFinalData = () => {
        const tempList = [...initialList];
        if (dataSource && dataSource.length > 0) {
            dataSource.forEach(item => {
                const i = item.value;
                tempList.splice(5 - i, 1, item);
            });
        }
        return tempList;
    };

    return (
        <Table
            className={styles.formTable}
            showHeader={false}
            rowKey="value"
            bordered={false}
            dataSource={getFinalData()}
            columns={editColumns}
            rowClassName={styles.editableRow}
            pagination={false}
            {...restProps}
        />
    );
};

export default EvaluationItemTable;
